﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TpBaseObjects.Objects;
using TpBaseObjects.Interfaces;
using TpData.Manager;
using TpNotiz.Forms;
using TpNotiz.Interfaces;
using TpTools;
using TpNotiz.Properties;

namespace TpNotiz.Controller {
    public class TecNotizController : IController {

        #region Members

        private TecNotiz myForm;
        private bool sendMails = false;

        #endregion

        #region Constructor

        public TecNotizController(ITecForm form) {
            myForm = form as TecNotiz;
        }

        #endregion

        #region Methods

        public void Dispose() {
        }

        public void Close(object sender, EventArgs e) {
            this.myForm.Close();
        }

        public void FormClose(object sender, FormClosingEventArgs e) {
            if(myForm.MyModel != null) {
                if (myForm.MyModel.IsDirty
                    ) {
                    DialogResult result = MessageBox.Show("Daten haben sich geändert. Wollen Sie die Daten speichern?", myForm.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                    switch (result) {
                        case DialogResult.Yes:
                            myForm.MyModel.IsDirty = false;
                            Save(myForm.MyModel);
                            break;
                        case DialogResult.No:
                            myForm.MyModel.IsDirty = false;
                            break;
                        case DialogResult.Cancel:
                            e.Cancel = true;
                            break;
                    }
                }
            }
        }

        public void LoadModels() {
            myForm.MyOrgModel = OrganisationDataManager.LoadOrganisation(myForm.MyModel.NotizOrgKey);
            myForm.MyNotizStatusList = TecNotizDataManager.LoadTecNotizStates();
            myForm.MyNotizTelefonTypes = TecNotizDataManager.LoadTecNotizTelTypes();
            myForm.MyTargetUserModel = TecNotizDataManager.LoadTargetUser();
            if (myForm.MyOrgModel == null) {
                myForm.MyOrgModel = new Organisation();
            }
            if (myForm.MyModel != null) {
                if(myForm.MyModel.ZugeordneteUser != null) {
                    if (myForm.MyModel.ZugeordneteUser.Count > 0) {
                        foreach (TecNotizTargetUser tu in myForm.MyTargetUserModel) {
                            foreach (TecNotizTargetUser ttu in myForm.MyModel.ZugeordneteUser) {
                                if (tu.LoginName.Trim() == ttu.LoginName.Trim()) {
                                    tu.InUse = true;
                                }
                            }
                        }
                    }
                }
            }
        }

        public void Save(IBaseObject myObject) {
            TecNotizObj model = myObject as TecNotizObj;
            model.ClearTargetUser();
            foreach (TecNotizTargetUser u in myForm.MyTargetUserModel) {
                if (u.InUse) {
                    model.AddTargetUser(u);
                    if (!TecUserDataManager.CheckUserIsLoggedIn(u.TecUserID)) {
                        if (!TecNotizDataManager.GetNotizUserSentMails(u.TecUserID, model.NotizID) && sendMails) {
                            SendNotizViaMail(model, u);
                            TecNotizDataManager.SaveTecNotizUserSentMails(u.TecUserID, model.NotizID);
                        }
                    }
                }
            }
            TecNotizDataManager.SaveTecNotiz(model);
            myForm.MyModel.IsDirty = false;
        }

        private void SendNotizViaMail(TecNotizObj n, TecNotizTargetUser u) {
            Office.SendEmail(u.EMail, String.Format(Resources.NotizEmailBetreff, n.NameAnrufer), n.Notiz);
        }

        public void SetControlValues() {
            myForm.AnrufType = myForm.MyModel.AnrufType;
            myForm.MyStatus = myForm.MyModel.Status;
        }

        public void SetControlsTextChanged(object sender, EventArgs e) {
            
        }

        #endregion

    }
}
